package com.xhixm.chatbot.domain.zsxq.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhixm.chatbot.domain.zsxq.model.aggregates.UnAnsweredQuestionsAggregates;
import com.xhixm.chatbot.domain.zsxq.model.req.AnswerReq;
import com.xhixm.chatbot.domain.zsxq.model.req.ReqData;
import com.xhixm.chatbot.domain.zsxq.model.res.AnswerRes;
import com.xhixm.chatbot.domain.zsxq.IZsxqApi;
import com.xhixm.chatbot.domain.zsxq.model.res.RespData;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ZsxqApi implements IZsxqApi {


    private Logger logger = LoggerFactory.getLogger(ZsxqApi.class);
    @Override
    public UnAnsweredQuestionsAggregates queryUnAnsweredQuestionsTopicId(String groupId, String cookie) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet("https://api.zsxq.com/v2/groups/"+groupId+"/topics?scope=unanswered_questions&count=20");
        httpGet.addHeader("cookie",cookie);
        httpGet.addHeader("content-type","application/json;charset=UTF-8");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            String res = EntityUtils.toString(response.getEntity());

            logger.info("拉取问题：groupId：{},res：{}",groupId,res);
            return JSON.parseObject(res,UnAnsweredQuestionsAggregates.class);
        }else {
            throw new RuntimeException("queryUnAnsweredQuestionsTopicId Err Code is"+response.getStatusLine().getStatusCode());
        }
    }

    @Override
    public boolean answer(String groupId, String cookie, long topicId, String text, boolean silenced) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost("https://api.zsxq.com/v2/topics/"+topicId+"/answer");
        httpPost.addHeader("cookie",cookie);
        httpPost.addHeader("content-type","application/json;charset=UTF-8");
        httpPost.addHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36");

        /*String paramJson = "{\n" +
                "  \"req_data\": {\n" +
                "    \"text\": \"不好！\\n\",\n" +
                "    \"image_ids\": [],\n" +
                "    \"silenced\": true\n" +
                "  }\n" +
                "}";
        */


        AnswerReq answerReq = new AnswerReq(new ReqData(text, silenced));

        String paramJson = JSONObject.toJSONString(answerReq);


        StringEntity stringEntity = new StringEntity(paramJson, ContentType.create("text/json", "UTF-8"));

        httpPost.setEntity(stringEntity);

        CloseableHttpResponse execute = httpClient.execute(httpPost);

        if (execute.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            String res = EntityUtils.toString(execute.getEntity());
            logger.info("回答问题：groupId{},topicId {},res {}",groupId,topicId,res);
            AnswerRes answerRes = JSON.parseObject(res, AnswerRes.class);
            return answerRes.isSucceeded();
        }else {
            throw new RuntimeException("queryUnAnsweredQuestionsTopicId Err Code is"+execute.getStatusLine().getStatusCode());
        }
    }

}
